//
//  AddThingViewController.m
//  smarthings
//
//  Created by Stefan Tsvyatkov on 9/1/14.
//  Copyright (c) 2014 Stefan Tsvyatkov. All rights reserved.
//

#import "ThingDetailsViewController.h"
#import "PropertiesActionsViewController.h"
#import "SetupInformationViewController.h"
#import <MBProgressHUD.h>

@interface ThingDetailsViewController() <SmarthingsDelegate>

- (void)updateScreen;

@property (weak, nonatomic) IBOutlet UITableViewCell *nameCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *propertiesCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *actionsCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *setupInfoCell;

@property(nonatomic, strong) Smarthings *smarthingsSDK;

@end

@implementation ThingDetailsViewController

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        _thing = nil;
        _smarthingsSDK = [Smarthings new];
        _smarthingsSDK.delegate = self;
        _isNewThing = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isNewThing) {
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonTapped)];
        self.navigationItem.leftBarButtonItem = cancelButton;
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped)];
        self.navigationItem.rightBarButtonItem = doneButton;
        self.nameCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.nameCell.userInteractionEnabled = YES;
        self.setupInfoCell.hidden = YES;
    }
    else {
        self.nameCell.accessoryType = UITableViewCellAccessoryNone;
        self.nameCell.userInteractionEnabled = NO;
        self.setupInfoCell.hidden = NO;
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [self updateScreen];
}

- (void)updateScreen {
    self.navigationItem.title = self.thing.name;
    self.nameCell.detailTextLabel.text = self.thing.name;
    if (self.thing.properties.count > 0) {
        self.propertiesCell.detailTextLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.thing.properties.count];
    }
    else {
        self.propertiesCell.detailTextLabel.text = @"None";
    }
    if (self.thing.actions.count > 0) {
        self.actionsCell.detailTextLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)self.thing.actions.count];
    }
    else {
        self.actionsCell.detailTextLabel.text = @"None";
    }
}

- (void)cancelButtonTapped {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)doneButtonTapped {
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [self.smarthingsSDK addThing:self.thing];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Thing's Name" message:@"Please enter thing's name" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            [alert addButtonWithTitle:@"OK"];
            alert.tag = 0;
            [alert show];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 0) {
        if (buttonIndex == 1) {
            self.thing.name = [alertView textFieldAtIndex:0].text;
            [self updateScreen];
        }
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ActionsList"]) {
        [(PropertiesActionsViewController *)[segue destinationViewController] setThing:self.thing];
        [(PropertiesActionsViewController *)[segue destinationViewController] setIsNewThing:self.isNewThing];
        [(PropertiesActionsViewController *)[segue destinationViewController] setDataType:LDTActions];
    }
    if ([segue.identifier isEqualToString:@"PropertiesList"]) {
        [(PropertiesActionsViewController *)[segue destinationViewController] setThing:self.thing];
        [(PropertiesActionsViewController *)[segue destinationViewController] setIsNewThing:self.isNewThing];
        [(PropertiesActionsViewController *)[segue destinationViewController] setDataType:LDTProperties];
    }
    if ([segue.identifier isEqualToString:@"SetupInformation"]) {
        [(SetupInformationViewController *)[segue destinationViewController] setThing:self.thing];
    }
}

#pragma mark - Smarthings Delegate

- (void)addThingFinishedWithFailure:(NSError *)error {
    [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Adding a Thing"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}

- (void)addThingFinishedWithSuccess:(SMTThing *)thing {
    [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
